import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution937 {
    public String[] reorderLogFiles(String[] logs) {
        List<String> alfa=new ArrayList<>();
        List<String> nums=new ArrayList<>();
        for (String log : logs) {
            String[] strings=log.split(" ");
            if (strings[1].charAt(0)>='0' && strings[1].charAt(0)<='9'){
                nums.add(log);
            }else{
                alfa.add(log);
            }
        }
        alfa.sort((a,b)->{
            int aIn=a.indexOf(" ");
            int bIn=b.indexOf(" ");
            if (a.substring(aIn+1).equals(b.substring(bIn+1))){
                return a.substring(0,aIn).compareTo(b.substring(0,bIn));
            }else{
                return a.substring(aIn+1).compareTo(b.substring(bIn+1));
            }
        });
        int cur=0;
        String[] ans=new String[logs.length];
        for (String s : alfa) {
            ans[cur]=s;
            cur++;
        }
        for (String num : nums) {
            ans[cur]=num;
            cur++;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution937().reorderLogFiles(new String[]{"a1 9 2 3 1", "g1 act car", "zo4 4 7", "ab1 off key dog", "a8 act zoo"})));
    }
}
